<template>
  <div class="mail-mail-edit">
    <!-- 添加或修改邮件发送对话框 -->
    <el-dialog v-model="isShowDialog" width="800px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.mail-mail-edit .el-dialog', '.mail-mail-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'邮件发送'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="邮箱类型" prop="mType">
          <el-input v-model="formData.mType" placeholder="请选择邮箱类型" />
        </el-form-item>
        <el-form-item label="用户名称" prop="user">
          <el-input v-model="formData.user" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input v-model="formData.pass" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="主机地址" prop="host">
          <el-input v-model="formData.host" placeholder="请输入主机地址" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input v-model="formData.port" type="number" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1" >启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listPluginMail,
  getPluginMail,
  delPluginMail,
  addPluginMail,
  updatePluginMail,
} from "/@/api/plugins/mail/mail";
import {
  PluginMailTableColumns,
  PluginMailInfoData,
  PluginMailTableDataState,
  PluginMailEditState
} from "/@/views/plugins/mail/mail/list/component/model"
export default defineComponent({
  name:"apiV1MailPluginMailEdit",
  components:{
  },
  props:{
  },
  setup(props,{emit}) {
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();
    const state = reactive<PluginMailEditState>({
      loading:false,
      isShowDialog: false,
      formData: {
        id: undefined,
        mType: undefined,
        user: undefined,
        pass: undefined,
        host: undefined,
        port: undefined,
        status: false ,
        remark: undefined,
      },
      // 表单校验
      rules: {
        id : [
            { required: true, message: "不能为空", trigger: "blur" }
        ],
        status : [
            { required: true, message: "状态 : 0 - 禁用 1 - 启用不能为空", trigger: "blur" }
        ],
      }
    });
    // 打开弹窗
    const openDialog = (row?: PluginMailInfoData) => {
      resetForm();
      if(row) {
        getPluginMail(row.id!).then((res:any)=>{
          const data = res.data;
          data.mType = ''+data.mType
          data.status = parseInt(data.status)
          state.formData = data;
      })
    }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addPluginMail(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('mailList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updatePluginMail(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('mailList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {
        id: undefined,
        mType: undefined,
        user: undefined,
        pass: undefined,
        host: undefined,
        port: undefined,
        status: false ,
        remark: undefined,
      }
    };
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>
  .kv-label{margin-bottom: 15px;font-size: 14px;}
  .mini-btn i.el-icon{margin: unset;}
  .kv-row{margin-bottom: 12px;}
</style>
